//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.Verifications
{
    using System;

    /// <summary>
    /// Describes required configuration verification
    /// </summary>
    public class ConfigurationVerificationDescription : VerificationDescription
    {
        /// <summary>
        /// Gets or Sets function to retreive configuration value using a specified key
        /// </summary>
        public Func<string, string> GetConfiguration
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or Sets the configuration key to check
        /// </summary>
        public string ConfigurationKey
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or Sets the configuration value's expected type (Optional if CanBeConvertedTo is set)
        /// </summary>
        public Type ExpectedType
        {
            get;
            set;
        }

        /// <summary>
        /// (Optional) Gets or Sets an override function to check whether the value can be converted to expected type
        /// </summary>
        public Func<string, bool> CanBeConvertedTo
        {
            get;
            set;
        }

        protected override void ValidateState()
        {
            if (this.GetConfiguration == null)
            {
                throw new MissingDescriptionValueException("GetConfiguration");
            }

            if (string.IsNullOrEmpty(this.ConfigurationKey))
            {
                throw new MissingDescriptionValueException("ConfigurationKey");
            }

            if (this.ExpectedType == null && this.CanBeConvertedTo == null)
            {
                throw new MissingDescriptionValueException("ExpectedType");
            }
        }
    }
}
